package com.anjufang.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.anjufang.entity.AdminLoginLog;
import com.anjufang.entity.AdminUser;
import com.anjufang.service.system.UserTokenService;
import com.anjufang.utils.IPUtil;
import com.anjufang.utils.Result;
import com.anjufang.utils.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;


/**
 * 登录 Action
 * 
 * @author weichengyu
 *
 */
@RestController
public class LoginController extends BaseController {

	/*@Autowired
	private Producer producer;*/

	@Autowired
	@Qualifier("sysUserToken")
	private UserTokenService userTokenService;

	/**
	 * 验证码
	 * 
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	/*@RequestMapping("captcha.jpg")
	public void captcha(HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");
		// 生成文字验证码
		String text = producer.createText();
		// 生成图片验证码
		BufferedImage image = producer.createImage(text);
		// 保存到shiro session
		ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}*/

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login/login", method = RequestMethod.POST)
	public Result login(String username, String password, String captcha, HttpServletRequest request) throws IOException {

		/*String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
		if (!captcha.equalsIgnoreCase(kaptcha)) {
			return Result.error("验证码不正确");
		}*/

		// 用户信息
		AdminUser user = new AdminUser();
		user = user.selectOne("user_name = {0}", username);

		// 账号不存在、密码错误
		if (user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
			return Result.error("账号或密码不正确");
		}

		// 账号锁定
		if (user.getStatus() == 0) {
			return Result.error("账号已被锁定,请联系管理员");
		}

		// 登录日志
		AdminLoginLog loginLog = new AdminLoginLog();
		loginLog.setAccount(user.getUserName());
		loginLog.setTime(new Date());
		loginLog.setIp(IPUtil.getIpAdrress(request));
		loginLog.insert();

		// 生成token，并保存到数据库
		Result r = userTokenService.createToken(user.getId(), "pc");
		return r.put("userName", user.getUserName());
	}

}
